package com.touchportfolio.view
{
	import com.greensock.TweenLite;
	import com.touchportfolio.model.ApplicationModel;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	
	import traer.physics.Spring;

	public class SettingsMenuView extends Sprite
	{
		private var _settingsmenu:SettingsMenu;
		private var _stage:Stage;
		private var _sTransform:SoundTransform;
		
		public function SettingsMenuView(stage:Stage) 
		{
			_stage = stage;
			_settingsmenu = new SettingsMenu();
			_settingsmenu.x = stage.stageWidth-40;
			//_settingsmenu.x = stage.stageWidth - _settingsmenu.width - 10;
			_settingsmenu.y = stage.stageHeight - (_settingsmenu.height + 50);
			addChild(_settingsmenu);
			_sTransform = new SoundTransform(1,0);
			_settingsmenu.SoundButton.gotoAndPlay(2);

			
			_settingsmenu.SettingsButton.buttonMode = true;
			_settingsmenu.FullscreenButton.buttonMode = true;
			_settingsmenu.SoundButton.buttonMode = true;
			
			_settingsmenu.SettingsButton.addEventListener(MouseEvent.CLICK,settingsclick);
			_settingsmenu.FullscreenButton.addEventListener(MouseEvent.CLICK,fullscreenclick);
			_settingsmenu.SoundButton.addEventListener(MouseEvent.CLICK,soundclick);
			
			ApplicationModel.getInstance().addEventListener(ApplicationModel.FULLSCREEN_CHANGE,fullscreenModel);
			ApplicationModel.getInstance().addEventListener(ApplicationModel.SOUND_CHANGE,soundchangeModel);
			ApplicationModel.getInstance().addEventListener(ApplicationModel.SETTING_CHANGE,settingschangeModel);
			
			drawrects(0x6190a4,14,13,0,0);
			drawrects(0x518564,14,13,20,0);
			drawrects(0x383634,14,13,41,0);
			drawrects(0x735a44,14,13,61,0);
			
			_settingsmenu.color1.addEventListener(MouseEvent.CLICK,clickRect);
			_settingsmenu.color2.addEventListener(MouseEvent.CLICK,clickRect);
			_settingsmenu.color3.addEventListener(MouseEvent.CLICK,clickRect);
			_settingsmenu.color4.addEventListener(MouseEvent.CLICK,clickRect);
			
			_settingsmenu.color1.buttonMode = true;
			_settingsmenu.color2.buttonMode = true;
			_settingsmenu.color3.buttonMode = true;
			_settingsmenu.color4.buttonMode = true;
		
		}
		
		private function drawrects(color:Number,width:Number,height:Number,x:Number,y:Number):void{
				var rect:Shape = new Shape();
				rect.graphics.beginFill(color);
				rect.graphics.drawRect(x, y, width, height);
				rect.graphics.endFill();
				_settingsmenu.blokjes.addChild(rect);
				_settingsmenu.blokjes.buttonMode = true;
		}
		
		private function clickRect(e:Event):void{
			switch(e.currentTarget.name){
				case "color1":
					ApplicationModel.getInstance().color = 0x6190a4;
					break;
				case "color2":
					ApplicationModel.getInstance().color = 0x518564;
					break;
				case "color3":
					ApplicationModel.getInstance().color = 0x585858;
					break;
				case "color4":
					ApplicationModel.getInstance().color = 0x735a44;
					break;
			}
		}
		
		private function soundclick(e:Event):void{
			ApplicationModel.getInstance().sound = true;
		}
		
		private function fullscreenclick(e:Event):void{
			ApplicationModel.getInstance().fullscreen = true;
		}
		
		private function fullscreenModel(e:Event):void{
			if(ApplicationModel.getInstance().fullscreen == true){
				_settingsmenu.FullscreenButton.gotoAndPlay(2);
				_stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
			}else{
				_settingsmenu.FullscreenButton.gotoAndPlay(1);
				_stage.displayState = StageDisplayState.NORMAL;
			}
		}
		
		private function soundchangeModel(e:Event):void{
			if(ApplicationModel.getInstance().sound == true){
				_settingsmenu.SoundButton.gotoAndPlay(1);
				_sTransform.volume = 0;
				SoundMixer.soundTransform = _sTransform;
			}else{
				_settingsmenu.SoundButton.gotoAndPlay(2);
				_sTransform.volume = 1;
				SoundMixer.soundTransform = _sTransform;
			}
		}
		
		private function settingschangeModel(e:Event):void{
			if(ApplicationModel.getInstance().settings == true){
				TweenLite.to(_settingsmenu, .3, {x: stage.stageWidth - _settingsmenu.width - 10});
			}else{
				TweenLite.to(_settingsmenu, .3, {x: stage.stageWidth - 40});
			}
		}
		
		private function settingsclick(e:Event):void{
			ApplicationModel.getInstance().settings = true;
		}
		
		
	}
}